package com.szs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szs.common.CustomException;
import com.szs.common.R;
import com.szs.mapper.CategoryMapper;
import com.szs.pojo.Category;
import com.szs.pojo.Dish;
import com.szs.pojo.Setmeal;
import com.szs.service.CategoryService;
import com.szs.service.DishService;
import com.szs.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public R<String> add(Category category) {
        log.info("新增分类信息：{}", category);
        this.save(category);
        return R.success("新增分类成功");
    }

    @Override
    public R<Page> page(int page, int pageSize) {

        log.info("page = {},pageSize = {}", page, pageSize);

        // 创建分页对象
        Page pageInfo = new Page(page, pageSize);
        // 创建条件创造器
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();

        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);

        // 执行查询
        super.page(pageInfo, categoryLambdaQueryWrapper);

        return R.success(pageInfo);
    }

    @Override
    public R<String> remove(Long id) {

        log.info("分类-删除id：{}", id);

        // 查看当前分类有无菜品，有则无法删除
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("当前分类下存在菜品，不能删除");
        }

        // 查看当前套餐有无菜品，有则无法删除
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2 > 0) {
            throw new CustomException("当前套餐下存在菜品，不能删除");
        }

        super.removeById(id);
        return R.success("删除成功");
    }

    @Override
    public R<String> update(Category category) {
        log.info("修改的信息为：{}", category);
        super.updateById(category);
        return R.success("修改成功");
    }

    @Override
    public R<List<Category>> list(Category category) {
        // 获取查询对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        // 根据type查询
        queryWrapper.eq(category.getType() != null, Category::getType, category.getType());

        // 创建排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = this.list(queryWrapper);

        return R.success(list);
    }
}
